#include "global.h"
#include "mydatetable.h"

#include <QMenu>
#include <QAction>
#include <QHeaderView>

// 第5-6列属于周末
#define WEEKEND_COLUMN_BEGIN    (5)
#define WEEKEND_COLUMN_END    (6)

MyDateTable::MyDateTable(QWidget *parent) : MyTableWidget(parent)
{
    setParent(parent);
}

void MyDateTable::setDateHorizontalHeaderStyle(QFont headerFont, QString styleSheet, bool headFixed)
{
    setHorizontalHeaderStyle( headerFont,  styleSheet,  headFixed);
    // 随文字变更表头大小
   horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
   return;
}

void MyDateTable::createItemAndSetStyle(int row, int col)
{
    QTableWidgetItem *pItem = createItem("", row, col);

    setItemStyle(QBrush(Qt::black), QFont("Microsoft YaHei", 11), row, col);

    resetItemBackground(pItem, col);
    return;
}

void MyDateTable::resetItemBackground(QTableWidgetItem *pItem, int col)
{
    if(NULL == pItem){
        QLOG_WARN() << "NULL == pItem";
        return;
    }

    if(col >= WEEKEND_COLUMN_BEGIN && col <= WEEKEND_COLUMN_END)
        pItem->setBackground(QBrush(DATESET_TABLE_HOLIDAY_COLOR));
    else
        pItem->setBackground(QBrush(DATESET_TABLE_WORKDAY_COLOR));

    return;
}

void MyDateTable::setDateTableWidgetStyle(void)
{
    // 不显示行号
    verticalHeader()->setVisible(false);

    setTableWidgetStyle();
    return;
}

void MyDateTable::paintCalender(int row, int col, QString text)
{
    if(row >= this->rowCount())
        this->addNewRow(row);

    QTableWidgetItem *pItem = item(row, col);
    if(NULL == pItem){
        QLOG_WARN() << "NULL == pItem";
        return;
    }

    pItem->setText(text);
    return;
}

void MyDateTable::clearCalender(void)
{
    for(int i = 0; i < this->rowCount(); i++){
        for(int j = 0; j < this->columnCount(); j++){
            QTableWidgetItem *pItem = item(i, j);
            if(NULL == pItem){
                QLOG_WARN() << "NULL == pItem";
                continue;
            }

            pItem->setText("");
            resetItemBackground(pItem, j);
        }
    }

    return;
}

void MyDateTable::addNewRow(int row)
{
    setRowCount(rowCount() + 1);
    // 创建各单元格item
    for (int j  = 0; j < columnCount(); j++){
        createItemAndSetStyle(row, j);
    }

    return;
}

void MyDateTable::createActions(void)
 {
    // 允许QTableWidget接收QWidget::customContextMenuRequested()信号。
    setContextMenuPolicy(Qt::CustomContextMenu);

   //创建菜单项
    pMenu = new QMenu(this);
    QMenu *pSubMenu = new QMenu("改变本日类型", this);
    QAction *pWorkDay = new QAction("普通工作日", this);
    QAction *pHoliday = new QAction("普通节假日", this);
    QAction *pWorkDayAndSpecifyDay = new QAction("工作日和统一监控", this);
    QAction *pHolidayAndSpecifyDay = new QAction("节假日和统一监控", this);
    QMenu *pSubMenu2 = new QMenu("设置本日工作人员", this);
    QAction *pDay2 = new QAction("白班", this);
    QAction *pNight2 = new QAction("夜班", this);
    QMenu *pSubMenu3 = new QMenu("设置本日非工作人员", this);
    QAction *pDay3 = new QAction("白班", this);
    QAction *pNight3 = new QAction("夜班", this);

    pSubMenu->addAction(pWorkDay);
    pSubMenu->addAction(pHoliday);
    pSubMenu->addAction(pWorkDayAndSpecifyDay);
    pSubMenu->addAction(pHolidayAndSpecifyDay);
    pMenu->addMenu(pSubMenu);
    pSubMenu2->addAction(pDay2);
    pSubMenu2->addAction(pNight2);
    pMenu->addMenu(pSubMenu2);
    pSubMenu3->addAction(pDay3);
    pSubMenu3->addAction(pNight3);
    pMenu->addMenu(pSubMenu3);

    // 监听action的triggered信号，将参数pos发送至DateSet
    connect(pWorkDay, &QAction::triggered, this, [=](){emit signal_changeDayAction_triggered(menuPos, DATESET_WORKDAY);});
    connect(pHoliday, &QAction::triggered, this,  [=](){emit signal_changeDayAction_triggered(menuPos, DATESET_HOLIDAY);});
    connect(pWorkDayAndSpecifyDay, &QAction::triggered, this, [=](){emit signal_changeDayAction_triggered(menuPos, DATESET_WORKDAY_AND_SPECIFYDAY);});
    connect(pHolidayAndSpecifyDay, &QAction::triggered, this, [=](){emit signal_changeDayAction_triggered(menuPos, DATESET_HOLIDAY_AND_SPECIFYDAY);});
    connect(pDay2, &QAction::triggered, this, [=](){emit signal_changeWorkAction_triggered(menuPos, DATESET_DAYWORK);});
    connect(pNight2, &QAction::triggered, this, [=](){emit signal_changeWorkAction_triggered(menuPos, DATESET_NIGHTWORK);});
    connect(pDay3, &QAction::triggered, this, [=](){emit signal_changeWorkAction_triggered(menuPos, DATESET_NODAYWORK);});
    connect(pNight3, &QAction::triggered, this, [=](){emit signal_changeWorkAction_triggered(menuPos, DATESET_NONIGHTWORK);});

    // 监听QWidget::customContextMenuRequested信号
    connect(this, &QWidget::customContextMenuRequested, this, &MyDateTable::slot_menu_customContextMenuRequested);

    return;
}

void MyDateTable::changeDateItemBackground(int row, int col, int daytype)
{
    QTableWidgetItem *pItem = item(row, col);
    if(NULL == pItem){
        QLOG_WARN() << "NULL == pItem";
        return;
    }

    QBrush foreBrush= pItem->foreground();
    QBrush backBrush= pItem->foreground();

    if (daytype == DATESET_WORKDAY)
        backBrush = QBrush(QColor(DATESET_TABLE_WORKDAY_COLOR));
    else if (daytype == DATESET_HOLIDAY)
        backBrush = QBrush(QColor(DATESET_TABLE_HOLIDAY_COLOR));
    else if (daytype == DATESET_WORKDAY_AND_SPECIFYDAY || daytype == DATESET_HOLIDAY_AND_SPECIFYDAY)
        backBrush = QBrush(QColor(DATESET_TABLE_SPECIFYDAY_COLOR));
    else{
        QLOG_WARN() << "err daytype" << daytype;
        return;
    }

    pItem->setForeground(foreBrush);
    pItem->setBackground(backBrush);
    return;
}

int MyDateTable::getDateItemText(int row, int col, QString & string)
{
    QTableWidgetItem *pItem = item(row, col);
    if(NULL == pItem){
        QLOG_WARN() << "NULL == pItem";
        return -1;
    }

    string = pItem->text();

    return 0;
}

void MyDateTable::slot_menu_customContextMenuRequested(const QPoint &pos)
{
    QTableWidgetItem *pItem = NULL;
    int row = 0, col = 0;

    pItem = this->getItemRowAndColFromPos(pos, row, col);
    if( NULL  == pItem){
        QLOG_WARN() << "clicked out of table!";
        return;
    }

    // 如果是空字符，直接返回
    if("" == pItem->text())         return;

    menuPos = pos;
    pMenu->exec(QCursor::pos());
    return;
}

